package blackspire.action.login;

import java.util.List;
import java.util.Map;

import blackspire.model.character.WOWCharacter;
import blackspire.model.user.User;
import blackspire.service.character.CharacterService;
import blackspire.service.user.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
	private String userId;
	private String password;
	private UserService userService;
	private CharacterService characterService;
	private List<User> users;
	private List<WOWCharacter> characters;
	
	public LoginAction(UserService userService, CharacterService characterService)
	{
		this.userService = userService;
		this.characterService = characterService;
	}

	public String execute() throws Exception
	{
		this.users = userService.findAll();
		User user = userService.find(userId);

		if (user == null)
		{
			return ERROR;
		}

		if (password.equals(user.getPassword()))
		{
			Map session = ActionContext.getContext().getSession();
			session.put("logged-in", "true");
			return SUCCESS;
		} else
		{
			return ERROR;
		}
	}

	public String logout() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
		session.remove("logged-in");
		return SUCCESS;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getUserId()
	{
		return userId;
	}

	public void setUserId(String userId)
	{
		this.userId = userId;
	}
	
    public List<User> getUsers() {
        return users;
    }
    
    public List<WOWCharacter> getCharacters() {
    	
    	if (characters == null)
    	{
    		this.characters = characterService.findAll();
    	}
    	
        return characters;
    }
}